import fs from "fs";
import path from "path";

/**
 *
 * @param filePath 判断的文件路径
 */
function isFile(filePath: string): boolean {
  if (fs.existsSync(filePath)) {
    let state = fs.statSync(filePath);
    if (state.isDirectory()) {
      return false;
    } else {
      return true;
    }
  } else {
    return false;
  }
}

/**
 * 复制文件
 * @param filePath 被拷贝的文件路径
 * @param targetPath  拷贝的位置
 */
function copyFIle(filePath: string, targetPath: string): boolean {
  //判断是否存在
  if (!fs.existsSync(filePath)) {
    console.log("该文件不存在");
    return false;
  }
  //判断是否是文件夹
  let state = fs.statSync(filePath);
  if (state.isDirectory()) {
    console.log("不能直接复制目录");
    return false;
  }
  let str = fs.readFileSync(filePath, "utf-8");
  fs.writeFileSync(targetPath, str, "utf-8");
  return true;
}

/**
 *
 * @param resource 拷贝的目录
 * @param target  存放的位置
 */
function copyDIr(resource: string, target: string) {
  // 判断target存在
  if (!fs.existsSync(resource)) {
    console.log(`${resource}目录不存在`);
    return;
  }
  // 判断target存在
  if (!fs.existsSync(target)) {
    fs.mkdirSync(target);
  }
  fs.readdirSync(resource)
    .map((item) => {
      return {
        abs: path.join(resource, item),
        name: item,
      };
    })
    .forEach((val) => {
      if (isFile(val.abs)) {
        copyFIle(val.abs, path.join(target, val.name));
      } else {
        copyDIr(val.abs, path.join(target, val.name));
      }
    });
}

// function ceshi() {
//     let dirPath = path.join(__dirname, '../controlle')
//     if (!fs.existsSync(dirPath)) {
//         fs.mkdirSync(dirPath)
//     }
//     let fileList = fs.readdirSync(dirPath)
//     console.log(fileList)
//     // fs.unlinkSync('./index.txt')
// }
// ceshi()

function reName(sourceFile: string, targetFile: string) {
  // console.log(__dirname);
  const dicUrl = sourceFile.split("/");
  dicUrl.pop();
  if (sourceFile && targetFile) {
    fs.rename(
      path.join(sourceFile),
      path.join(dicUrl.join("/"), targetFile),
      function (err) {
        if (!err) {
        } else {
          console.log(err);
        }
      }
    );
  }
}

function writeJson(pathUrl: string, value: any) {
  return new Promise((resolve, reject) => {
    fs.writeFileSync(pathUrl, JSON.stringify(value), "utf8");
    resolve(true);
  });
}
function readJson<T>(path: string): Promise<T> {
  return new Promise((resolve, reject) => {
    const res = fs.readFileSync(path, "utf8");
    if (res) {
      resolve(JSON.parse(res));
    } else {
      resolve({} as T);
    }
  });
}
function deleteall(path: string) {
  var files = [];
  if (fs.existsSync(path)) {
    files = fs.readdirSync(path);
    files.forEach(function (file, index) {
      var curPath = path + "/" + file;
      if (fs.statSync(curPath).isDirectory()) {
        // recurse
        deleteall(curPath);
      } else {
        // delete file
        fs.unlinkSync(curPath);
      }
    });
    fs.rmdirSync(path);
  }
}

/**
 *
 * @param path 当为目录路径时会删除目录下的所有文件
 */
function deleteFile(path: string) {
  if (isFile(path)) {
    fs.unlinkSync(path);
  } else {
    deleteall(path);
    fs.mkdirSync(path);
  }
}

function deleteDir(path: string) {
  if (fs.existsSync(path)) {
    if (fs.statSync(path).isDirectory()) {
      fs.rmdirSync(path);
    }
  }
}

// deleteFile(path.join(__dirname, './upload'))

// reName()

export { copyDIr, copyFIle, isFile, reName, readJson, writeJson, deleteFile };
